﻿
$(document).ready(function () {
    //点击创建投票
    $("#addaction").on("click", function () {
        self.location.href = "/Setting/ActionDetails/0?a=new";
    });

    //点击删除活动
    $("a.delete").on("click", function () {
        var tr = $(this).parent().parent();
        var td = $(this).parent();
        var id = $(td).find("input[type='hidden']:last").val();
        //判断是否已经有人投票
        var voteCount = $(td).find("input[type='hidden']:first").val();
        var tiptext = (0 < voteCount) ? "已经有人投票确定要删除！" : '确定要删除此投票活动？';

        dialog({
            title: '提示',
            content: tiptext,
            okValue: '确定',
            ok: function () {
                this.title('提交中…');
                $.ajax({
                    type: "POST",
                    url: "/Setting/DeleteAction/",
                    data: { "actionId": id },
                    dataType: "json",
                    success: function (data) {
                        self.location.reload();
                    }
                });
                return false;
            },
            cancelValue: '取消',
            cancel: function () {
            }
        }).show();
    });

    //点击编辑活动
    $("a.edit").on("click", function () {
        var td = $(this).parent();
        var id = $(td).find("input[type='hidden']:last").val();
        var voteCount = $(td).find("input[type='hidden']:first").val();
        if (0 == voteCount) {
            self.location.href = "/Setting/ActionDetails/" + id + "?a=edit";
        }
        else {
            dialog({
                title: "提示",
                width: "10em",
                height: "1em",
                content: "已经有人投票不能编辑！",
            }).width(220).show();
        }
    });

    //通用分页
    var surffixIndex = self.location.href.lastIndexOf("/") + 1;
    var pageUrl = self.location.href.substring(0, surffixIndex);

    //下一页
    $("#nexPage").on("click", function () {
        pageIndex++;
        self.location.href = pageUrl + "?index=" + pageIndex;
    });

    // 下一页
    $("#prePage").on("click", function () {
        pageIndex--;
        if (0 == pageIndex) {
            var d = dialog({
                title: '提示',
                content: '已经是第一页了哦！'
            });
            d.show();
        }
        else {
            self.location.href = pageUrl + "?index=" + pageIndex;
        }
    });

    // 导出Excel
    $("#exorptExcel").on("click", function () {
        var actionlist = [];
        var selectedAction = $("input:checkbox[name=cbitem]:checked");
        if (selectedAction.length == 0) {
            var d = dialog({
                title: '提示',
                content: '请选择一项活动！',
            });
            d.show();
            return;
        }
        for (var i = 0; i < selectedAction.length; i++) {
            actionlist.push($(selectedAction[i]).attr("tip"));
        }

        var form = $("<form>");//定义一个form表单
        form.attr("style", "display:none");
        form.attr("target", "");
        form.attr("method", "post");
        form.attr("action", "/Setting/ExportResult/?actionlist=" + actionlist.join(","));
        var input1 = $("<input>");
        input1.attr("type", "hidden");
        input1.attr("name", "exportData");
        input1.attr("value", (new Date()).getMilliseconds());
        $("body").append(form);//将表单放置在web中
        form.append(input1);
        form.submit();
    });


    if ($("table.table>tbody>tr").length < pageItemCount) {
        if (0 == $("table.table>tbody>tr").length) {
            pageIndex--;
            if (0 == pageIndex) {
                pageIndex = 1;
            }
            var d = dialog({
                title: '提示',
                content: '已到最后一页了哦！',
                ok: function () {
                    self.location.href = pageUrl + "?index=" + pageIndex;
                }
            });
            d.show();

        }
    }

});

